package cn.touna.jss.modules.third.chexingyi;

import cn.touna.risk.api.contraints.facet.First;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.jss.common.properties.disconf.CheXingYiProperties;
import cn.touna.jss.common.util.DateUtils;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.third.chexingyi.entity.CheXingYiRequestEntity;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 车行易：车辆违章查询
 */
@Service
public class CheXingYiService {

    private static final Logger logger = LogManager.getLogger(CheXingYiService.class);

    @Autowired
    private CheXingYiProperties cheXingYiProperties;

    @ApiMapping(value = "chexingyi.queryindex",desc = "车行易.违章查询")
    public Response queryindex(CheXingYiRequestEntity entity) {

        logger.info("REQ 【车行易.违章查询】 queryindex--> paramMap：{}", entity);
        Response response = new Response();
        ValidUtil.checkRequestParams(entity, First.class);
        try {
            StringBuffer sb = new StringBuffer();
            sb.append(cheXingYiProperties.getServer());
            sb.append("/");
            sb.append(cheXingYiProperties.getMethod());
            sb.append("?userid=").append(cheXingYiProperties.getUserid());
            sb.append("&userpwd=").append(cheXingYiProperties.getUserpwd());
            sb.append("&cartype=").append(entity.getCarType() == null ? "02" : entity.getCarType());
            sb.append("&carnumber=").append(entity.getCarNumber());

            sb.append(StringUtils.isEmpty(entity.getCarCode())?"":"&carcode="+entity.getCarCode());
            sb.append(StringUtils.isEmpty(entity.getCarDriveNumber())?"":"&cardrivenumber="+entity.getCarDriveNumber());
            sb.append(StringUtils.isEmpty(entity.getProvinceId())?"":"&provinceid="+entity.getProvinceId());
            sb.append(StringUtils.isEmpty(entity.getCityId())?"":"&cityid="+entity.getCityId());
            sb.append("&timeaxis=").append(DateUtils.dateToStr(new Date(), "yyyyMMddhhmmss"));
            logger.info("【车行易.违章查询】 发出请求的参数信息--> getParam：{}", sb);
            String result = HttpUtil.getInstance().get(sb.toString());
            JSONObject json =  JSONObject.parseObject(result);
            String success  = json.getString("Success");
            String errMessage  = json.getString("ErrMessage");
            if("true".equals(success)){
                return response.success(SystemErrorCodeEnum.SYSTEM_REQUEST_SUCC,result);
            } else {
                return response.fail(SystemErrorCodeEnum.JSS_EXTSYSTEM_RESP_ERROR, errMessage, result);
            }
        } catch (Exception e) {
            logger.error("【车行易.违章查询】异常,queryindex--> paramMap：{}", entity, e);
            return response.fail(SystemErrorCodeEnum.JSS_CALL_EXTSYSTEM_ERROR);
        }
    }
}
